package org.hhchat.leetcode.dp;

/**
 * Created by HMH on 2017/3/31.
 *
 * leetcode 121
 */
public class code121 {
    public static class Solution {
        public int maxProfit(int[] prices) {
            int min = Integer.MAX_VALUE;
            int max = Integer.MIN_VALUE;
            int buy=0;
            int sell=0;
            for(int i=0;i<prices.length;i++) {
                if (prices[i] < min) {
                    min=prices[i];
                    buy=i;
                }
            }
            for(int i=buy;i<prices.length;i++) {
                if (prices[i] > max) {
                    max=prices[i];
                    sell=i;
                }
            }
            if(buy<sell)
                return max-min;
            else
                return 0;
        }
    }
    public static void main(String[] args){
        System.out.println(new Solution().maxProfit(new int[]{7,1,5,3,6,4}));
    }


}
